/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.ui.wizards;

import it.cums.core.controller.ApplicationController;
import it.cums.core.model.Country;
import it.cums.ui.localization.Messages;

import java.util.List;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class SupplierPersonalWizardPage extends WizardPage implements IWizardPage {
	private Text capText;
	private Text locationText;
	private Text addressText;
	private Combo countryCombo;
	private Text phoneText;
	private Text mobilePhoneText;
	private Text emailText;
	private Text webSiteText;
	private Composite container;
	
	public SupplierPersonalWizardPage() {
		super(Messages.SupplierPersonalWizardPage_0);
		setTitle(Messages.SupplierPersonalWizardPage_1);
		setDescription(Messages.SupplierPersonalWizardPage_2);
	}

	
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		Label label1 = new Label(container, SWT.NULL);
		label1.setText(Messages.SupplierPersonalWizardPage_3);
		
		addressText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		addressText.setLayoutData(gd);
		addressText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText(Messages.SupplierPersonalWizardPage_4);
		
		capText = new Text(container, SWT.BORDER | SWT.SINGLE);
		capText.setLayoutData(gd);
		capText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText(Messages.SupplierPersonalWizardPage_5);
		
		locationText = new Text(container, SWT.BORDER | SWT.SINGLE);
		locationText.setLayoutData(gd);
		locationText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText(Messages.SupplierPersonalWizardPage_6);
		
		countryCombo = new Combo(container, SWT.READ_ONLY);
		countryCombo.setLayoutData(gd);
		initCountries();
		countryCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
		});
		
		Label line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL | SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.horizontalSpan = layout.numColumns;
        gridData.verticalIndent = 9;
        line.setLayoutData(gridData);
        
        Label label5 = new Label(container, SWT.NULL);
		label5.setText(Messages.SupplierPersonalWizardPage_7);
		
		phoneText = new Text(container, SWT.BORDER | SWT.SINGLE);
		phoneText.setLayoutData(gd);
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText(Messages.SupplierPersonalWizardPage_8);
		
		mobilePhoneText = new Text(container, SWT.BORDER | SWT.SINGLE);
		mobilePhoneText.setLayoutData(gd);
		
		Label label7 = new Label(container, SWT.NULL);
		label7.setText(Messages.SupplierPersonalWizardPage_9);
		
		emailText = new Text(container, SWT.BORDER | SWT.SINGLE);
		emailText.setLayoutData(gd);
		
		Label label8 = new Label(container, SWT.NULL);
		label8.setText(Messages.SupplierPersonalWizardPage_10);
		
		webSiteText = new Text(container, SWT.BORDER | SWT.SINGLE);
		webSiteText.setLayoutData(gd);
		
		setControl(container);
		setPageComplete(false);
	}
	
	private void dialogChanged() {
		if (getAddress().length() == 0) {
			setDescription(Messages.SupplierPersonalWizardPage_11);
			setPageComplete(false);
		} else if (getCAP().length() == 0) {
			setDescription(Messages.SupplierPersonalWizardPage_12);
			setPageComplete(false);
		} else if (getLocation().length() == 0) {
			setDescription(Messages.SupplierPersonalWizardPage_13);
			setPageComplete(false);
		} else if (getCountry().length() == 0) {
			setDescription(Messages.SupplierPersonalWizardPage_14);
			setPageComplete(false);
		} else {
			setDescription(Messages.SupplierPersonalWizardPage_15);
			setPageComplete(true);
		}
	}
	
	public String getCAP() {
		return capText.getText().trim();
	}
	
	public String getLocation() {
		return locationText.getText().trim();
	}
	
	public String getAddress() {
		return addressText.getText().trim();
	}
	
	public String getCountry() {
		return countryCombo.getText().trim();
	}
	
	public String getPhone() {
		return phoneText.getText().trim();
	}
	
	public String getMobilePhone() {
		return mobilePhoneText.getText().trim();
	}
	
	public String getEmail() {
		return emailText.getText().trim();
	}
	
	public String getWebSite() {
		return webSiteText.getText().trim();
	}
	
	
	public Control getControl() {
		return container;
	}
	
	private void initCountries() {
		List<Country> countries = ApplicationController.getInstance().getCountries();
		for (Country country : countries) {
			countryCombo.add(country.getDescription());
		}
		countryCombo.add("", 0); //$NON-NLS-1$
	}
}
